<?php
	//微信支付回调页面
	require_once "config.php";
	$xml = $GLOBALS['HTTP_RAW_POST_DATA'];
	file_put_contents('test.txt', $xml);
	$Result = xmlToArray($xml);
	if($Result['return_code'] == 'SUCCESS'){
		$ReturnXml = array('return_code'=>'SUCCESS','return_msg'=>'OK');
	}else{
		$ReturnXml = array('return_code'=>'FAIL','return_msg'=>'签名失败');
	}
	echo arrayToXml($ReturnXml);
	if($Result['return_code'] == 'SUCCESS' && $Result['result_code'] == 'SUCCESS'){
		require_once "jssdk.php";
		$jssdk = new JSSDK();
		$signPackage = $jssdk->GetSignPackage();



		$sel_sql="select * from dyspw_order where order_num = '".$Result['out_trade_no']."'";
		$order = mysqli_query($mysql, $sel_sql);
		$order_info = $order->fetch_all(MYSQLI_ASSOC);

		$goods_order = $mysql->query("select * from dyspw_goods_order where ordernum = {$Result['out_trade_no']}");
		$goodsorder = $goods_order->fetch_all(MYSQLI_ASSOC);

		if(!empty($goodsorder)){//商品购买流程
			$Up_sql = "Update dyspw_goods_order set state = 1, transaction_id = {$Result['transaction_id']} where id = ".$goodsorder[0]['id'];
			$add_goods = mysqli_query($mysql, $Up_sql);

			//商品扣减库存
            if($goodsorder[0]['waimai'] && $goodsorder[0]['order_details']){
                //外卖商品库存更新
                $goodsInfo=json_decode($goodsorder[0]['order_details'],true);
                foreach ($goodsInfo as $val){
                    $sql='update dyspw_goods set sales = sales+'.$val['num'].' WHERE id ='.$val['id'];
                    mysqli_query($mysql, $sql);
                }
            }else{
                //团购商品库存更新
                // $sql = 'update dyspw_goods set sales = sales+'.$goodsorder[0]['number'].' , stock = stock-'.$goodsorder[0]['number'].' WHERE id ='.$goodsorder[0]['goods_id'];
				$sql = 'update dyspw_goods set sales = sales+'.$goodsorder[0]['number'].' where id = '.$goodsorder[0]['goods_id'];
                mysqli_query($mysql, $sql);
            }

			require_once "phpqrcode.php";
			//生成核销二维码流程
			define('UPLOAD_PATH', './images/');
			define('UPLOAD_URL', '/xiaochengxu/images/qr_code/orders_hexiao/');
			//二维码流程

			//判断是否存在可用文件夹，若没有可用则新建
			$file_path = UPLOAD_PATH . 'qr_code/orders_hexiao';
			if(!is_dir($file_path)){
				mkdir($file_path,777,true);
			}

			//进入生成二维码流程
			$qr_code_path = UPLOAD_PATH . 'qr_code/orders_hexiao/';//二维码保存的路径信息
			$url = 'https://'.$_SERVER['HTTP_HOST']."/xiaochengxu/hexiao.php?order_sn=".$Result['out_trade_no'];//定义二维码包含的文字信息内容
			$qr_code_file = $qr_code_path . $Result['out_trade_no'] . ".png";
			$size = 5;
			\QRcode::png($url, $qr_code_file, QR_ECLEVEL_M, $size);//png方法生成二维码并保存至指定文件夹

			$qr_url = 'https://'.$_SERVER['HTTP_HOST'] . UPLOAD_URL . $Result['out_trade_no'] . ".png";
			//二维码生成成功后，二维码地址入库操作
			$Up = "Update dyspw_goods_order set hexiao_qr = '".$qr_url."' where id = ".$goodsorder[0]['id'];
			$add_qrcode = mysqli_query($mysql, $Up);


		}else{//会员充值业务
			$time = time();
			$month_time = strtotime(date('Y-m-d ',strtotime('+1 month')));//获取一个月后的时间戳
			$year_time = strtotime(date('Y-m-d H:i:s',strtotime('+1 year')));//获取一年后的时间戳
			if($order_info[0]['recharge_type'] == 'month'){
				$end_time = $month_time;
			}elseif ($order_info[0]['recharge_type'] == 'year') {
				$end_time = $month_time;
			}
			$Up_sql = "Update dyspw_order set order_status = 1 where id = ".$order_info[0]['id'];
			$mem_sql = "Update dyspw_member set is_vip = ".$order_info[0]['is_vip'].", start_time = '".$time."', end_time = '".$end_time."' where id = ".$order_info[0]['uid'];
			$add_memshop = mysqli_query($mysql, $Up_sql);
			$add_order = mysqli_query($mysql, $mem_sql);
		}

        return ' <xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';

		// $minfo_sql="select * from dyspw_member where id = '".$order_info[0]['uid']."'";
		// $member = mysqli_query($mysql, $minfo_sql);
		// $member_info = $member->fetch_all(MYSQLI_ASSOC);
		// $_SESSION['Member_info'] = $member_info;

		// // $rechargeorderinfo = $this->rechargeorderinfo->where(array('trade_no'=>$Result['out_trade_no']))->find();
		//这里面写你需要的执行代码
	}

/**
 * 数组转为xml
 * @param $arr
 * @return string
 */
function arrayToXml($arr){
	$xml = "<xml>";
	foreach($arr as $key => $val){
		if(is_numeric($val)){
			$xml .= "<".$key.">".$val."</".$key.">";
		} else {
			$xml .= "<".$key."><![CDATA[".$val."]]></".$key.">";
		}
	}
	$xml .= "</xml>";
	return $xml;
}

/**
 *  作用：将xml转为array
 */
function xmlToArray($xml){
	//将XML转为array
	return json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
}
?>
